package com.yuyue.springFramework.note11.entity.custom1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ComponentScan
public class MyConfig1 {
    @Bean
    @Autowired(required = false)
    //注册事件广播器，并自动填充事件监听器
    public EventMulticaster eventMulticaster(List<EventListener> eventListeners){
        EventMulticaster eventPublisher = new SimpleEventMulticaster();

        if(eventListeners != null){
            //使用lambda表达式调用 EventMulticaster.addEventListener 方法添加监听器
            eventListeners.forEach(eventPublisher :: addEventListener);
        }

        return eventPublisher;
    }

    @Bean
    //注册用户注册bean，Spring会自动注入依赖的bean
    public UserRegisterService userRegisterService(EventMulticaster eventMulticaster){
        UserRegisterService userRegisterService = new UserRegisterService();
        userRegisterService.setEventMulticaster(eventMulticaster);
        return userRegisterService;
    }
}
